package br.com.timecardforpeon.sqlite.domain;

import static br.com.timecardforpeon.domain.Day.GET_BY_DAY_QUERY;
import static br.com.timecardforpeon.domain.Day.LIST_DAY_QUERY;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import br.com.timecardforpeon.domain.Day;
import br.com.timecardforpeon.sqlite.BasicDao;
import br.com.timecardforpeon.util.Util;

public class DayDao extends BasicDao<Day> {

	public DayDao(Context context) {
		super(context);
	}

	public List<Day> listDays(String... parameters) {
		
		try {
			
			return this.execute(LIST_DAY_QUERY);
		}
		catch (Exception e) {
			Log.e("timeCard4Peon", "Exception listDays", e);
		}
		
		return new ArrayList<Day>();
	}

	public Day getByDay(Date date) {
		
		try {
		
			return this.executeUniqueResult(GET_BY_DAY_QUERY, "" + Util.dateToString(date));
		}
		catch (Exception e) {
			Log.e("timeCard4Peon", "Exception getByDay", e);
		}
		
		return null;
	}

	@Override
	public Day cursorToDomain(Cursor cursor) throws ParseException {

		if (cursor.getCount() == 0) {
			return new Day();
		}

		Day day = new Day();
		day.setId(cursor.getLong(0));
		day.setDay(Util.stringToDate(cursor.getString(1)));
		day.setNotes(cursor.getString(2));

		return day;
	}
}